<template>
    <van-nav-bar
        title="退货申请表"
        left-text="返回"
        left-arrow
        @click-left="back"
    />

    <van-form @submit="refund">
        
        <van-cell-group inset>
            <van-field
                v-model="order.refundreason"
                name="refundreason"
                rows="2"
                autosize
                label="退货原因"
                type="textarea"
                maxlength="100"
                placeholder="请输入退货原因"
                show-word-limit
                id="refundreason"
            />
        </van-cell-group>

        <div style="margin: 16px;">
            <van-button round block type="primary" native-type="submit" v-if="order.status" disabled>
            已提交
            </van-button>
            <van-button round block type="primary" native-type="submit" v-else>
            提交
            </van-button>
        </div>
        
    </van-form>
</template>

<script setup>
    
    import {getCurrentInstance} from 'vue'  
    const {proxy}  = getCurrentInstance()

    var business = reactive(proxy.$cookies.get('business'))
    var orderid = proxy.$route.query.orderid ? proxy.$route.query.orderid : 0
    orderid = ref(orderid)
    let order = ref({})
    let status = false

    // 钩子函数
    onBeforeMount(() => {
        getinfo()
    })

    const back = () => {
        proxy.$router.go(-1)
        return false
    }


    // 获取订单和商品的信息
    let getinfo = async () => {
        var data = {
            orderid: orderid.value
        }

        var result = await proxy.$POST({
            url: '/order/getinfo',
            params: data
        })

        if(result.code == 0){
            proxy.$fail(result.msg)
            return false
        }else{
            order.value = result.data
            console.log(order.value);
            return false
        }
    }

    // 退货原因填写
    let refund = async (values) => {
        var data = {
            busid: business.id,
            orderid: orderid.value,
            refundreason: values.refundreason
        }

        // console.log(data);
        var result = await proxy.$POST({
            url: '/order/refund',
            params: data
        })

        if(result.code === 0){
            proxy.$fail(result.msg)
            return false
        }else{
            proxy.$success(result.msg)
            getinfo()
            return false
        }
    }


</script>

<style>
    .van-button{
        margin: 15px 0;
    }

</style>